/*
 * DefaultLog.java	2008/07/28
 *
 * Copyright 2008 Web & Zen Multimedia, All rights reserved.
 * Use is subject to license terms.
 */
package it.wzm.lightweightlog;

import java.util.Properties;

/**
 * <p>
 * The main accessor for logging our entry. It implements a singleton design.
 * So there is only one instance of Log in the whole application, by calling the Log.getLog() method,
 * a check is made to see if an instance of Log already exists. If yes it is returned, if not it is initialized.
 * </p>
 * <p>
 * By calling:<br/>
 * Log l = Log.getLog();<br/>
 * l.info(Object);<br/>
 * </p>
 * <p>
 * a property file, dump.properties is searched in the classpath then if not found the defaul one inside package will be taken.
 * Then...<br/>
 * -Transform file into properties<br/>
 * -Load global property<br/>
 * -Find class name of the Dumper to initialize<br/>
 * -Extrapolate property for dumper<br/>
 * -Initialize the Queue<br/>
 * -Start the QueueDumper if necessary<br/>
 *
 * @author Leonardo Celati
 * @version 1.0
 */
@SuppressWarnings("unchecked")
public final class DefaultLog extends Log {	
	
    /**
     * Default constructor
     */
    public DefaultLog(Properties p) {
    	super(p);
    }
    
    /**
     * Build an Entry from the passed Object and Store it in Queue
     * @param tolog Object to log
     * @param severity The level of Severity
     * @param t A throwable for getting the caller of this error
     */
    @Override
	protected  void add(Object tolog, Throwable t, Severity severity) {
        // log holder
        Entry e = null;
        // decide if we want to log a string or an exception
        if (tolog != null && tolog instanceof Exception) {
            e = new Entry(getName(),new Exception((Exception) tolog), severity, System.currentTimeMillis());
        } else {
            e = new Entry(getName(),tolog, severity, t.getStackTrace()[t.getStackTrace().length - 1].getClassName(), System.currentTimeMillis());
        }
        
        // finally add to queue...
        try {
			LogFactory.getQueue().push(e);
		} catch (QueueException e1) {
			
		} catch (NullPointerException e1) {
			
		}		
    }  
 
}